﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BookM.AddReviseform
{
    public partial class ReaderUpdateForm : Form
    {
        private string readerd;
        public ReaderUpdateForm()
        {
            InitializeComponent();
        
        }
        public ReaderUpdateForm(string readerid)
        {
            readerd = readerid;
            InitializeComponent();
        }

        ReaderManagementBLL readerbll = new ReaderManagementBLL();
        private void bt_AddBook_Click(object sender, EventArgs e)
        {
            if (Judge())
            {
                UpdateReader();
            }
        }

        /// <summary>
        /// 修改读者
        /// </summary>
        private void UpdateReader()
        {
            ReaderInfo readerInf = new ReaderInfo(txb_BookID.Text, txb_Name.Text, txb_Author.Text, txb_Money.Text, textBox1.Text);
            readerInf.Id = int.Parse(readerd);
            if (readerbll.UpdateReader(readerInf))
            {
                MessageBox.Show("修改成功");
                this.Close();
                DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("修改失败");
            }
        }


        /// <summary>
        /// 判断用户的值
        /// </summary>
        private bool Judge()
        {
            Regex reg = new Regex(@"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{2,6}$");
            if (txb_BookID.Text == "")
            {
                MessageBox.Show("读者编号不能为空");
                return false;
            }
            else if (!reg.IsMatch(txb_BookID.Text))
            {
                MessageBox.Show("请输入正确的读者编号");
                return false;
            }
            else if (txb_Name.Text=="")
            {
                MessageBox.Show("读者读者名称不能为空");
                return false;
            }
            else if (txb_Author.Text == "")
            {
                MessageBox.Show("读者邮箱不能为空");
                return false;
            }
            else if (txb_Money.Text == "")
            {
                MessageBox.Show("读者住址不能为空");
                return false;
            }
            else if (textBox1.Text == "")
            {
                MessageBox.Show("请输入读者手机号码");
                return false;
            }
            return true;
        }

        /// <summary>
        /// 窗体加载时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReaderUpdateForm_Load(object sender, EventArgs e)
        {
            NewMethod();
        }

        /// <summary>
        /// 
        /// </summary>
        private void NewMethod()
        {
            DataTable dt = readerbll.FindByReader(readerd);
            if (dt.Rows.Count > 0)
            {
                txb_BookID.Text = dt.Rows[0]["读者编号"].ToString();
                txb_Name.Text = dt.Rows[0]["读者姓名"].ToString();
                txb_Author.Text = dt.Rows[0]["读者邮箱"].ToString();
                txb_Money.Text = dt.Rows[0]["读者住址"].ToString();
                textBox1.Text = dt.Rows[0]["读者手机号码"].ToString();
            }
        }
    }
}
